<template>
	<view class="page">


		<view class="base mar-bottom66">
			<view class="flex align-center ">
				<!--  <u-icon
          @click="baseMore = true"
          v-if="!baseMore"
          name="arrow-down"
          size="14"
          color="#999999"
          labelPos="left"
          labelSize="14"
        ></u-icon>
        <u-icon
          @click="baseMore = false"
          v-else
          name="arrow-up"
          size="14"
          color="#999999"
          labelPos="left"
          labelSize="14"
        ></u-icon> -->
				<view class="cr-333 font_s32 font_w500 mar-left12">{{favorites.favoritesName}}</view>
				<view class="font_s26 cr-999 mar-left6"><text class="mar-right6">·</text>
					<text v-if="list.length">{{list.length}}</text>
					<text v-else>0</text>
				</view>
			</view>
			<view class="menu-box" v-if="baseMore">
				<view class="menu-item" v-for="item in list">
					<view class=" mar-bottom23 flex justify-between align-center">
						<view class="font_s26 cr-333"> {{item.contentItemName}} </view>
					</view>

					<view class="font_s24 cr-999"> 收藏时间 {{item.createTime}} </view>

				</view>
			</view>
		</view>
		<t-favoritespop :show='showFavoritesPopup' :defaultFavorites='activeFavorites' :batch='batch'
			:targetId='favorites.userId' :targetFavoritesld='favorites.id'
			@close='(defaultFavorites)=>{handleShowFaPopup(false);init();setDefaultFavorites(defaultFavorites)}'></t-favoritespop>
		<!-- 	<u-popup
	class="favorites-popup"
	  :show="showFavoritesPopup"
	  mode="bottom"
	  @close="handleUpdateContentCollectionStatus"
	  
	>
	  <view class="favorites-pop padd-top30 padd-left32 padd-right32">
	    <view class="flex justify-between mar-bottom31">
			<view class="font_s28 cr-333 font_w500 ">
				选择收藏夹
			</view>
			<view class="font_s24 cr-999" @click="handleShowCollctPopup(true)">
				<image src="/static/img/icon_add.png" mode="" class="w-21 h-21 mar-right10"></image>
				新建收藏夹
			</view>
		</view>
	    <view class="content ">
	      <view class=" mar-bottom19 item" v-for="item in favoritesList">
			 <radio-group  @change="radioChange">
						<label class="flex justify-between"  >
							
							<view>
								<view class="cr-333 font_s26 mar-bottom17">
									{{item.favoritesName}}
								</view>
								<view class="cr-999 font_s24">
									10个内容
								</view>
							</view>
							<view>
								<radio  :value="`${item.id}`" :checked="activeFavorites?activeFavorites.id==item.id:false" />
							</view>
						</label>
					
			</radio-group>
	      </view>
	    </view>
	  </view>
	</u-popup> -->
		<button class="btn" @click="handleClickCollectAll">全部收藏</button>
	</view>
</template>

<script>
	const app = getApp().globalData

	function queryStringToObject(queryString) {
		// 创建一个空对象以存储结果
		const obj = {};

		// 移除字符串中的"?"前缀 (如果有的话)
		queryString = queryString.replace(/^\?/, "");

		// 分割查询字符串获取键值对
		const pairs = queryString.split("&");

		// 循环遍历键值对
		for (let i = 0; i < pairs.length; i++) {
			const pair = pairs[i];
			// 分割每一对以获取键和值
			const parts = pair.split("=");

			// 如果分割成功，则将键值对加入到对象中
			if (parts.length === 2) {
				// 为了确保键和值都是正确的字符串形式，可以使用decodeURIComponent进行解码
				const key = decodeURIComponent(parts[0]);
				const value = decodeURIComponent(parts[1]);

				// 对于值，我们也可以尝试转换成数字（如果适用的话）
				obj[key] = isNaN(value) ? value : Number(value);
			}
		}
		return obj;
	}


	export default {
		data() {
			return {
				baseMore: true,
				favorites: null,
				list: [],
				// favoritesList:[],
				faChild: null,
				activeFavorites: null,
				showFavoritesPopup: false,
				batch: false,
				// showCeateCollectPopup:false
			};
		},
		onLoad(query) {
      let queryStr;
      if(query.scene){
         queryStr = decodeURIComponent(query.scene);
      }
			if (queryStr) {
				app.scene = queryStr
			}


			if (app.scene) {
				this.favorites = queryStringToObject(app.scene);
				this.favorites.id = this.favorites.favoritesId
				this.init()
			}
		},

		methods: {
			init() {

				this.getUserFavoritesInfo()
				this.getFaChildren()
			},

			//获取收藏夹
			// getUserFavorites() {
			// 	return this.$http.get(
			// 		`/yaolexue-service-portal/userFavorites/findUserFavorites`, {}, {
			// 			isNeedLoading: true
			// 		}).then(res => {
			// 		this.favoritesList = res.data
			// 		this.activeFavorites = res.data.find(item =>item.id== this.favorites.id) || res.data[
			// 			0] 
			// 	})

			// },
			//获取收藏夹信息

			getUserFavoritesInfo() {

				return this.$http.get(
					`/yaolexue-service-portal/userFavorites/findUserFavoritesInfo/${this.favorites.id}`, {}, {
						isNeedLoading: true
					}).then(res => {
					this.favorites = {
						...this.favorites,
						...res.data
					}
					console.log(this.faChild, 'this.faChild')
				})
			},
			//加载收藏夹子项

			getFaChildren() {

				return this.$http.get(
					`/yaolexue-service-portal/userFavorites/findUserFavoritesItemByIdNoAuth`, {
						...this.favorites,
      
         
					}, {
						isNeedLoading: true
					}).then(res => {
					this.list = res.data
				})
			},
			//更新收藏夹
			// handleUpdateContentCollectionStatus(collectionStatus=1){
			// 		const collectionParams={
			// 				collectionStatus:collectionStatus,
			// 				contentItemId:this.faChild.id,

			// 		}
			// 		if(this.favorites){
			// 			collectionParams.userFavoritesId=+this.favorites.id
			// 		}
			// 	return this.$http.put(
			// 		`/yaolexue-service-portal/contentItem/updateContentCollectionStatus`, collectionParams, {
			// 			isNeedLoading: true
			// 		}).then(res=>{

			// 			if(res.code===200){
			// 				this.init()
			// 			}
			// 		})
			// },

			// //选择收藏夹
			// radioChange(evt){
			// 	this.activeFavorites=this.favoritesList.find(item=>item.id+''===evt.detail.value)

			// },
			//设置上一次收藏到的收藏夹
			setDefaultFavorites(activeFavorites) {
				this.activeFavorites = activeFavorites
			},
			//点击收藏全部
			handleClickCollectAll() {
				//没有登录就跳转到到登录之后登陆成功返回到此页面
				const token = uni.getStorageSync('token')
				if (token) {
					this.batch = true
					this.handleShowFaPopup(true)
				} else {
					uni.showModal({
						title: '提示',
						content: '请先登录',
						success: function(res) {
							if (res.confirm) {
								uni.navigateTo({
									url: '/pages/login/login?redirect=/pages/CollectShare/CollectShare'
								})
							} else if (res.cancel) {
								console.log('用户点击取消');
							}
						}
					});
				}

			},
			//显示收藏夹弹窗
			handleShowFaPopup(flag) {
				if (flag) {
					// this.getUserFavorites()
					// this.activeFavorites = this.favoritesList.find(item => item.defaultStatus) ||this.favoritesList[
					// 	0] //这里不能这样是因为有很多地方会操作activeFavorites
				} else {
					this.batch = false
					// this.activeFavorites =null
				}
				this.showFavoritesPopup = flag;
			},
			// handleShowCollctPopup(flag) {

			// 	this.showCeateCollectPopup = flag;
			// 	this.favoritesName = ''
			// },
		},
	};
</script>

<style lang="scss">
	.btn {
		width: 561rpx;
		height: 86rpx;
		line-height: 86rpx;
		background: #FA312A !important;
		border-radius: 43rpx !important;
		font-size: 28rpx;
		font-weight: 500;
		color: #FFFFFF;
		position: fixed;
		bottom: 95rpx;
		left: 50%;
		transform: translateX(-50%);
	}

	.page {
		padding: 41rpx 31rpx;
	}

	.base {
		.menu-box {
			width: 689rpx;
			background: #FFFFFF;
			margin: 20rpx auto;

			.menu-item {
				width: 100%;
				padding-bottom: 30rpx;
				padding-top: 30rpx;
				box-sizing: border-box;
				border-bottom: 1rpx solid rgba(240, 240, 240, 0.8);
			}
		}
	}

	.action-btn-box {
		position: relative;

		.u-icon {
			transform: rotateZ(270deg);
			color: #999999 !important;
		}

		.tip,
		.u-tooltip {
			position: absolute;
			top: 0;
			left: 0;
			/* z-index: -1; */
			width: 50%;
			/* opacity: 0; */
			/* overflow: hidden; */
		}

		::v-deep .tip-icon {
			.u-icon__icon {
				color: #999999 !important;
			}
		}
	}

	.his-pop {
		.content {
			width: 400rpx;
		}
	}

	.createcollect-pop {
		width: 400rpx;


	}
</style>